/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.action;

import east2west.dao.CarModelDAO;
import east2west.dao.CarTypeDAO;
import east2west.dto.CarModelDTO;
import east2west.dto.CarOrderDTO;
import east2west.dto.CarTypesDTO;
import east2west.dto.CarsDTO;
import east2west.service.CarModelService;
import east2west.service.CarOrderService;
import east2west.service.CarService;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author anhtuan
 */
public class AdminCarAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String ERR = "err";
    private final static String TRUE = "true";
    private final static String INSERT = "insert";
    private final static String MODEL = "model";

    public ActionForward listCar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        ArrayList<CarsDTO> listCar = serCar.readByAll();
        request.setAttribute("listCar", listCar);
        request.setAttribute("page", "carAdd.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward addnewCar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        ArrayList<CarModelDTO> listModel = serCar.readByAllModel();
        ArrayList<CarTypesDTO> listType = serCar.readByAllType();
        request.setAttribute("listModel", listModel);
        request.setAttribute("listType", listType);
        request.setAttribute("page", "carAddnew.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward editCar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        int id = Integer.parseInt(request.getParameter("carId"));
        CarsDTO carDTO = serCar.findById(id);
        ArrayList<CarModelDTO> listModel = serCar.readByAllModel();
        ArrayList<CarTypesDTO> listType = serCar.readByAllType();
        request.setAttribute("listModel", listModel);
        request.setAttribute("listType", listType);
        request.setAttribute("carDTO", carDTO);
        request.setAttribute("page", "carEdit.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward UpdateCar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        CarsDTO carDTO = new CarsDTO();
        carDTO.setCarId(Integer.parseInt(request.getParameter("carId")));
        carDTO.setCarName(request.getParameter("carName"));
        carDTO.setDetail(request.getParameter("detail"));
        carDTO.setModelId(Integer.parseInt(request.getParameter("model")));
        carDTO.setTypeId(Integer.parseInt(request.getParameter("type")));
        carDTO.setAirConditional(Byte.parseByte(request.getParameter("air")));
        carDTO.setQuantity(Integer.parseInt(request.getParameter("quantity")));
        carDTO.setSeatQty(Integer.parseInt(request.getParameter("seatQty")));
        carDTO.setPrice(Float.parseFloat(request.getParameter("price")));

        if (serCar.updateCar(carDTO)) {
            return mapping.findForward(TRUE);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward deleteCar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        int id = Integer.parseInt(request.getParameter("carId"));
        CarsDTO carDTO = serCar.findById(id);
        request.setAttribute("carDTO", carDTO);
        request.setAttribute("page", "carDelete.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward DeleteCar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        int id = Integer.parseInt(request.getParameter("carId"));
        if (serCar.deleteCar(id)) {
            return mapping.findForward(TRUE);
        } else {
            return mapping.findForward(ERR);
        }
    }

    /**
    CarType
     */
    public ActionForward listCarType(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        ArrayList<CarTypesDTO> listCarType = serCar.readByAllType();
        request.setAttribute("listCar", listCarType);
        request.setAttribute("page", "carType.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward insertCartype(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarTypesDTO dto = new CarTypesDTO();
        CarTypeDAO dao = new CarTypeDAO();
        dto.setTypeName(request.getParameter("frtypename"));

        if (dao.create(dto)) {
            return mapping.findForward(INSERT);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward deleteCartType(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("frtypeidd"));
        CarTypeDAO dao = new CarTypeDAO();
        if (dao.delete(id)) {
            return mapping.findForward(INSERT);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward updateCarType(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarTypesDTO dto = new CarTypesDTO();
        CarTypeDAO dao = new CarTypeDAO();
        dto.setTypeId(Integer.parseInt(request.getParameter("frtypeidd")));
        dto.setTypeName(request.getParameter("frtypename"));

        if (dao.update(dto)) {
            return mapping.findForward(INSERT);
        } else {
            return mapping.findForward(ERR);
        }
    }

    /**
    CarModel
     */
    public ActionForward listCarModel(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarModelService ser = new CarModelService();
        ArrayList<CarModelDTO> list = ser.readByAll();
        request.setAttribute("list", list);
        request.setAttribute("page", "carModel.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward insertCarmodel(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarModelDTO dto = new CarModelDTO();
        CarModelDAO dao = new CarModelDAO();
        dto.setModelName(request.getParameter("frmodelname"));
        if (dao.create(dto)) {
            return mapping.findForward(MODEL);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward deleteCartmodel(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("frmodelidd"));
        CarModelDAO dao = new CarModelDAO();
        if (dao.delete(id)) {
            return mapping.findForward(MODEL);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward updateCarmodel(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarModelDTO dto = new CarModelDTO();
        CarModelDAO dao = new CarModelDAO();
        dto.setModelId(Integer.parseInt(request.getParameter("frmodelidd")));
        dto.setModelName(request.getParameter("frmodelname"));

        if (dao.update(dto)) {
            return mapping.findForward(MODEL);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward listCarOrder(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarOrderService serCarOrder = new CarOrderService();
        ArrayList<CarOrderDTO> orderDTO = serCarOrder.readOrder();
        request.setAttribute("orderDTO", orderDTO);
        request.setAttribute("page", "listCarOrder.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward acceptCarOrder(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));
        CarOrderService serCarOrder = new CarOrderService();
        if (serCarOrder.accept(id)) {
            ArrayList<CarOrderDTO> orderDTO = serCarOrder.readOrder();
            request.setAttribute("orderDTO", orderDTO);
        }
        request.setAttribute("page", "listCarOrder.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward removeCarOrder(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));
        CarOrderService serCarOrder = new CarOrderService();
        if (serCarOrder.delete(id)) {
            ArrayList<CarOrderDTO> orderDTO = serCarOrder.readOrder();
            request.setAttribute("orderDTO", orderDTO);
        }
        request.setAttribute("page", "listCarOrder.page");
        return mapping.findForward(SUCCESS);
    }
}


